package com.esri.core.ogc.wmts;

import com.esri.core.ags.LOD;
import com.esri.core.ags.TileInfo;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.tasks.e.a.f;
import com.esri.core.internal.tasks.e.a.g;
import com.esri.core.ogc.OwsDescription;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import org.codehaus.jackson.util.MinimalPrettyPrinter;
import org.xml.sax.Attributes;

/* loaded from: classes.dex */
public class WMTSTileMatrixSet extends OwsDescription {
    private String a;
    private WMTSTileMatrix b;
    private g c;
    private String d;
    private SpatialReference e;
    private Map<String, WMTSTileMatrix> f = new HashMap();
    private TreeSet<WMTSTileMatrix> g = new TreeSet<>(new TileMatrixComparator());

    /* loaded from: classes.dex */
    public class TileMatrixComparator implements Comparator<WMTSTileMatrix> {
        public TileMatrixComparator() {
        }

        @Override // java.util.Comparator
        public int compare(WMTSTileMatrix wMTSTileMatrix, WMTSTileMatrix wMTSTileMatrix2) {
            MathContext mathContext = new MathContext(6);
            return new BigDecimal(wMTSTileMatrix.getScaleDenominator(), mathContext).compareTo(new BigDecimal(wMTSTileMatrix2.getScaleDenominator(), mathContext)) * (-1);
        }
    }

    public WMTSTileMatrixSet(Envelope envelope, TileInfo tileInfo, String str) {
        this.a = str;
        this.e = tileInfo.getSpatialReference();
        g gVar = new g();
        this.c = gVar;
        gVar.a(envelope);
        this.c.a(g.a(this.e, (List<String>) null));
        a(tileInfo);
    }

    public WMTSTileMatrixSet() {
    }

    public String getIdentifier() {
        return this.a;
    }

    public SpatialReference getSupportedCRS() {
        return this.e;
    }

    public Envelope getExtent() {
        g gVar = this.c;
        return gVar != null ? gVar.a() : new Envelope();
    }

    public List<WMTSTileMatrix> getTileMatrices() {
        return new ArrayList(this.g);
    }

    public WMTSTileMatrix getTileMatrix(String str) {
        return this.f.get(str);
    }

    @Override // com.esri.core.ogc.OwsItem
    public void startElement(String str, Attributes attributes) {
        super.startElement(str, attributes);
        if (this.b != null && !"TileMatrix".equals(str)) {
            this.b.startElement(str, attributes);
        } else if ("TileMatrix".equals(str)) {
            this.b = new WMTSTileMatrix();
            String str2 = this.d;
            if (str2 == null || !g.a(f.d, str2)) {
                return;
            }
            this.b.a();
        } else if ("BoundingBox".equals(str)) {
            g gVar = new g();
            this.c = gVar;
            gVar.a(new Envelope());
            a(attributes);
        }
    }

    @Override // com.esri.core.ogc.OwsDescription, com.esri.core.ogc.OwsItem
    public void endElement(String str) {
        WMTSTileMatrix wMTSTileMatrix;
        String currentTagName = getCurrentTagName();
        if (this.b != null && !"TileMatrix".equals(currentTagName)) {
            this.b.endElement(str);
        } else if (currentTagName.equals("Identifier")) {
            b(this.mData.toString().trim());
        } else if (currentTagName.equals("LowerCorner")) {
            e(this.mData.toString());
        } else if (currentTagName.equals("UpperCorner")) {
            d(this.mData.toString());
        } else if ("SupportedCRS".equals(currentTagName)) {
            a(this.mData.toString());
        } else if ("TileMatrix".equals(currentTagName) && (wMTSTileMatrix = this.b) != null) {
            this.f.put(wMTSTileMatrix.getIdentifier(), this.b);
            this.g.add(this.b);
            this.b = null;
        }
        super.endElement(str);
    }

    @Override // com.esri.core.ogc.OwsItem
    public void characters(char[] cArr, int i, int i2) {
        WMTSTileMatrix wMTSTileMatrix = this.b;
        if (wMTSTileMatrix != null) {
            wMTSTileMatrix.characters(cArr, i, i2);
        }
        super.characters(cArr, i, i2);
    }

    private void a(String str) {
        String c = c(str);
        this.d = c;
        this.e = SpatialReference.create(g.b(c));
        if (this.b == null || !g.a(f.d, this.d)) {
            return;
        }
        this.b.a();
    }

    private void b(String str) {
        if (str == null || str.length() <= 0) {
            return;
        }
        this.a = str;
    }

    private void a(Attributes attributes) {
        g gVar = this.c;
        if (gVar != null) {
            gVar.a(c(attributes.getValue("crs")));
        }
    }

    private String c(String str) {
        if (str.toLowerCase().contains("epsg")) {
            String[] split = str.split(":");
            int i = 0;
            try {
                i = Integer.parseInt(split[split.length - 1]);
            } catch (NumberFormatException unused) {
            }
            if (i == 900913) {
                i = SpatialReference.WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE_10;
            }
            if (i > 0) {
                return "EPSG:" + i;
            }
        }
        return "";
    }

    private void d(String str) {
        g gVar = this.c;
        if (gVar != null) {
            Envelope a = gVar.a();
            String[] split = str.split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
            if (split.length == 2) {
                a.setXMax(Double.parseDouble(split[0]));
                a.setYMax(Double.parseDouble(split[1]));
            }
        }
    }

    private void e(String str) {
        g gVar = this.c;
        if (gVar != null) {
            Envelope a = gVar.a();
            String[] split = str.split(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR);
            if (split.length == 2) {
                a.setXMin(Double.parseDouble(split[0]));
                a.setYMin(Double.parseDouble(split[1]));
            }
        }
    }

    private void a(TileInfo tileInfo) {
        for (LOD lod : tileInfo.getLODs()) {
            WMTSTileMatrix wMTSTileMatrix = new WMTSTileMatrix(tileInfo, lod);
            this.f.put(lod.getLevelValue(), wMTSTileMatrix);
            this.g.add(wMTSTileMatrix);
        }
    }
}
